let Api = require('../../utils/api.js');
let wxRequest = require('../../utils/wxRequest.js');

Page({
  data: {
    token: '',
    date: '',
    dateId: '',
    isdateshow: false,
    datecolumns: [],
    time: '',
    timeId: '',
    istimeshow: false,
    timecolumns: []
  },
  onLoad: function (options) {
    let user = wx.getStorageSync('user')
    if (user) {
      this.setData({
        token: user.code,
      })
      this.setDate()
      this.setMonth()
      this.query()
    } else {
      wx.navigateTo({
        url: '../login/login',
      })
    }
  },
  setDate: function () {
    var date = new Date()
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let list = new Array()
    for (let i = 2019; i <= year; i++) {
      let item = {}
      item.id = i
      item.text = i + '年'
      list.push(item)
    }
    this.setData({
      date: year + '年',
      dateId: year,
      datecolumns: list,
      time: month + '月',
      timeId: month
    })
    console.log(1, this.data)
  },
  setMonth: function () {
    let list = new Array()
    for (let i = 1; i <= 12; i++) {
      let item = {}
      item.id = i
      item.text = i + '月'
      list.push(item)
    }
    this.setData({
      timecolumns: list
    })
  },
  showdatetap: function () {
    this.setData({
      isdateshow: true
    })
  },
  showtimetap: function () {
    this.setData({
      istimeshow: true
    })
  },
  ondatecancel: function () {
    this.setData({
      isdateshow: false
    })
  },
  ontimecancel: function () {
    this.setData({
      istimeshow: false
    })
  },
  ondateconfirm: function (e) {
    var value = e.detail.value
    this.setData({
      date: value.text,
      dateId: value.id,
      isdateshow: false
    })
    this.query()
  },
  ontimeconfirm: function (e) {
    var value = e.detail.value
    this.setData({
      time: value.text,
      timeId: value.id,
      istimeshow: false
    })
    this.query()
  },
  query: function () {
    let self = this
    let year = self.data.date
    let month = self.data.time
    wx.showLoading({
      title: '正在查询...',
    })
    let paramurl = "&c=2&y=" + year + "&m=" + month
    wxRequest.getRequest(Api.agTx(self.data.token, paramurl), null).then(res => {
      if (res.data) {
        let adt = res.data
      } else {
        wx.showModal({
          title: '提示',
          content: '查询数据失败!'
        })
      }
    }).finally(fina => {
      wx.hideLoading()
    })
  }
})